﻿using e_commerce.BLL;
using e_commerce.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace e_commerce.Controllers
{
    public class BackAccountController : Controller
    {
        private readonly AccountService _acserver ;
        private readonly PermissionsService _pService;
 
        public BackAccountController()
        {
            _acserver = new AccountService();
            _pService = new PermissionsService();
        }
        public ActionResult Admin()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Admin(string account, string password)
        {
            try
            {
                var employee = _acserver.EmployeeLogin(account, password);
                AdminUser admin = new AdminUser()
                {
                    Name = employee.User.NickName,
                    Id = employee.Id,
                    UserId = employee.UserId,
                    Gender = employee.User.Gender,
                    Birthday = employee.User.Birthday,
                    CreateTime = employee.User.CreateTime,
                    PhoneNumber = employee.User.PhoneNumber,
                    Email = employee.User.Email,
                };
                Session["ADMIN"] = admin;
                Session["Permissions"] = _pService.GetEmployeePermissions(admin.Id);
                return RedirectToAction("Home", "Admin");
            }
            catch (Exception ex)
            {
                TempData["LoginError"] = ex.Message;
                return View();
            }
        }


        public ActionResult AdminLogout()
        {
            Session["ADMIN"] = null;
            return RedirectToAction("Admin");
        }
    }
}